package cn.xd.base.domain.entity;

import cn.xd.base.basic.type.*;
import cn.xd.base.domain.bean.command.AppUserThirdPartyCmd;
import cn.xd.core.basic.common.CreateTime;
import cn.xd.core.ddd.entity.Entity;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 移动端用户与第三方应用关联表
 * </p>
 *
 * @author chenyh
 * @since 2023-09-08
 */
@Builder
@Data
public class AppUserThirdParty implements Entity<AppUserThirdPartyId> {

    private AppUserThirdPartyId id;

    /**
     * 用户id
     */
    private GlobalAppUserId userId;

    /**
     * 开放id
     */
    private OpenId openId;

    /**
     * 唯一id
     */
    private UnionId unionId;

    /**
     * 类型
     */
    private ThirdPartyType type;

    /**
     * 应用类型
     */
    private AppUserType appUserType;

    /**
     * 创建时间
     */
    private CreateTime createTime;

    public static List<AppUserThirdParty> add(AppUserThirdPartyCmd cmd) {
        List<AppUserThirdParty> thirdParties = new ArrayList<>();
        thirdParties.add(AppUserThirdParty.builder().openId(OpenId.of(cmd.getOpenId())).createTime(CreateTime.now())
                .unionId(UnionId.of(cmd.getUnionId())).type(ThirdPartyType.of(cmd.getType())).appUserType(AppUserType.of(cmd.getAppUserType())).build());
        return thirdParties;
    }

    public static AppUserThirdParty add(GlobalAppUserId userId, String openId, String unionId, Integer type, Integer appUserType) {
        return AppUserThirdParty.builder().userId(userId).openId(OpenId.of(openId))
                .unionId(UnionId.of(unionId)).type(ThirdPartyType.of(type)).appUserType(AppUserType.of(appUserType))
                .createTime(CreateTime.now())
                .build();
    }
}

